package com.test.design.material.materialdesign;

import android.content.Context;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.LayoutAnimationController;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import view.MyRecyclerView;

/**
 * author:harisucici on 2018/7/4 15:42
 * email:harisucici@gmail.com
 * name:
 * todo:
 */
public class ImageActivity extends AppCompatActivity {
    private ImageView imageView;
    private int duration = 1000;
    private Interpolator interpolator = new DecelerateInterpolator();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("测试测试");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        
        imageView = (ImageView) findViewById(R.id.image2);

        List<String> dataList = new ArrayList<>(2);
        for(int i= 0;i<50;i++){
            dataList.add("测试测试列表"+i);
        }

        MyRecyclerView myRecyclerView = (MyRecyclerView)findViewById(R.id.recyclerview_image);
        ImageAdapter imageAdapter = new ImageAdapter(this,dataList);
        myRecyclerView.setAdapter(imageAdapter);
        //recycler 动画
        runLayoutAnimation(myRecyclerView);

    }

    // 触发动画
    private void runLayoutAnimation(RecyclerView recyclerView) {
        Context context = recyclerView.getContext();
        LayoutAnimationController controller =
                AnimationUtils.loadLayoutAnimation(context, R.anim.layout_animation_slide_right);
        recyclerView.setLayoutAnimation(controller);
        recyclerView.getAdapter().notifyDataSetChanged();
        recyclerView.scheduleLayoutAnimation();
    }



    private void animatorProfile() {
        int height  =-imageView.getHeight();
        imageView.setTranslationY(height);
        imageView.animate().translationY(0).
                setDuration(duration).setStartDelay(300).setInterpolator(interpolator);
        
    }

    //添加窗体在视图初始化完成过后
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            //开启动画
            animatorProfile();
        }
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home){
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }
}
